﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace SRTToolsV1
{
    public class AutoTrStr
    {
        public List<StrLineInfo> tSourInfoLines { get; } = null;
        public Dictionary<int, StrLineInfo> tCNInfos { get; private set; } = null;

        public int Count { get; private set; } = 0;
        public AutoTrStr(List<StrLineInfo> tSourInfoLines)
        {
            this.tSourInfoLines = tSourInfoLines;
        }

        public void AutoRun(Dictionary<int, StrLineInfo> tDestCNInfos)
        {
            Count = 0;
            if ((tSourInfoLines != null) && (tDestCNInfos != null))
            {
                tCNInfos = new Dictionary<int, StrLineInfo>(tDestCNInfos);
                Thread thdRun= new Thread(thdMainRun);
                thdRun.Start();
            }
        }

        public void thdMainRun()
        {
            Count = 0;
            try
            {
                if ((tSourInfoLines != null) && (tCNInfos != null) && (tCNInfos.Count > 0) && (tSourInfoLines.Count > 0))
                {
                    foreach (KeyValuePair<int, StrLineInfo> item in tCNInfos)
                    {
                        string sSour = tSourInfoLines[item.Value.index].context;
                        foreach (StrLineInfo line in tSourInfoLines)
                            if (line.context.Equals(sSour))
                            {
                                line.context = item.Value.context;
                                Count++;
                            }
                    }
                }
            }
            finally
            {
                tCNInfos = null;
            }
        }

        public async Task<int> AutoTRRun(Dictionary<int, StrLineInfo> tDestCNInfos)
        {
            int iDef = 0;
            int iCount = 0;
            if ((tSourInfoLines != null) && (tDestCNInfos != null))
            {
                if ((tSourInfoLines.Count > 0) && (tSourInfoLines[0].index == 1))
                    iDef = -1;
                tCNInfos = new Dictionary<int, StrLineInfo>(tDestCNInfos);
                try
                {
                    if ((tCNInfos.Count > 0) && (tSourInfoLines.Count > 0))
                    {
                        foreach (KeyValuePair<int, StrLineInfo> item in tCNInfos)
                            if (item.Value.index + iDef < tSourInfoLines.Count)
                            {
                                if (!tSourInfoLines[item.Value.index + iDef].hasTranslation)
                                {
                                    string sSour = tSourInfoLines[item.Value.index + iDef].context;
                                    foreach (StrLineInfo line in tSourInfoLines)
                                        if ((!line.hasTranslation) && line.context.Equals(sSour))
                                        {
                                            line.setTranslation(item.Value.context);
                                            iCount++;
                                        }
                                }
                            }
                    }
                }
                finally
                {
                    tCNInfos = null;
                }
            }
            return iCount;
        }

        public async Task<int> AutoASSRun(Dictionary<int, StrLineInfo> tDestCNInfos)
        {
            int iDef = 0;
            int iCount = 0;
            if ((tSourInfoLines != null) && (tDestCNInfos != null))
            {
                if ((tSourInfoLines.Count > 0) && (tSourInfoLines[0].index == 1))
                    iDef = -1;
                tCNInfos = new Dictionary<int, StrLineInfo>(tDestCNInfos);
                try
                {
                    if ((tCNInfos.Count > 0) && (tSourInfoLines.Count > 0))
                    {
                        foreach (KeyValuePair<int, StrLineInfo> item in tCNInfos)
                            if (item.Value.index + iDef < tSourInfoLines.Count)
                            {
                                if (!tSourInfoLines[item.Value.index + iDef].hasTranslation)
                                {
                                    string sSour = tSourInfoLines[item.Value.index + iDef].context;
                                    foreach (StrLineInfo line in tSourInfoLines)
                                        if ((!line.hasTranslation) && line.context.Equals(sSour))
                                        {
                                            line.setTranslation(item.Value.context);
                                            iCount++;
                                        }
                                }
                            }
                    }
                }
                finally
                {
                    tCNInfos = null;
                }
            }
            return iCount;
        }
    }
}
